/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import bill.model.vo.Customer;
import bill.model.vo.Inventory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class Config {

    //public static final String INVENTORY_DATA_FILE = "data/inventory.properties";
    public static final String INVENTORY_DATA_FILE = "data/inventory.xls";
    public static final String CUSTOMER_DATA_FILE = "data/customer.properties";
    public static final String PERSON_DATA_FILE = "data/person.properties";
    public static final String BUSI_DATA_FILE = "data/business.properties";
    public static final String DB_CONFIG_FILE = "config/db.properties";
    public static final String INV_CONFIG_FILE = "config/inventory.properties";
    private final static Properties db_config = new Properties();
    private final static Properties cust_config = new Properties();
    private final static Properties inve_config = new Properties();
    private final static Properties person_config = new Properties();
    private final static Properties busi_config = new Properties();
    public final static String CUS_CODE_KEY = "id";
    public final static String CUS_NAME_KEY = "name";
    private static InputStream dbis;
    private static InputStream cuis;
    private static InputStream invis;
    private static InputStream peris;
    private static InputStream busis;
    private final static Customer c = new Customer();
    private static final ArrayList<Inventory> inventories = new ArrayList<Inventory>();

    public static String getInvProperty(String key){
        try {
            try{
                invis = new FileInputStream(INV_CONFIG_FILE);
                inve_config.load(invis);
            }finally{
                if(invis != null) invis = null;
                invis = null;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return inve_config.getProperty(key);
    }
  

    public static String getDbProperty(String key) {
        try {
            try {
                dbis = new FileInputStream(DB_CONFIG_FILE);
                db_config.load(dbis);
            } finally {
                if (dbis != null) {
                    dbis.close();
                    dbis = null;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return db_config.getProperty(key);
    }

    
     public static String getBusiProperty(String key) {
        try {
            try {
                busis = new FileInputStream(BUSI_DATA_FILE);
                busi_config.load(busis);
            } finally {
                if (busis != null) {
                    busis.close();
                    busis = null;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return busi_config.getProperty(key);
    }

    public static Customer getCustomer() {
        try {
            try {
                cuis = new FileInputStream(CUSTOMER_DATA_FILE);
                cust_config.load(cuis);
                c.setId(StringUtils.ans2GB(cust_config.getProperty(CUS_CODE_KEY)));
                c.setName(StringUtils.ans2GB(cust_config.getProperty(CUS_NAME_KEY)));
            } finally {
                if (cuis != null) {
                    cuis.close();
                    cuis = null;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return c;
    }

    public static String getPersonProp(String key) {
        try {
            try {
                peris = new FileInputStream(PERSON_DATA_FILE);
                person_config.load(peris);
            } finally {
                if (peris != null) {
                    peris.close();
                    peris = null;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return person_config.getProperty(key);
    }

    public static void writeConfig(String fileName, String comment, Properties p) {
        OutputStreamWriter osw = null;
        try {
            try {
                osw = new OutputStreamWriter(new FileOutputStream(fileName), "GB18030");
                p.store(osw, new String(comment.getBytes(), "GB18030"));
            } finally {
                if (osw != null) {
                    osw.close();
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /*public static ArrayList<Inventory> getInventorFromConfig() {
        try {
            try {
                inventories.clear();
                invis = new FileInputStream(INVENTORY_DATA_FILE);
                inve_config.load(invis);
                Enumeration keys = inve_config.keys();
                int num = 0;
                while(keys.hasMoreElements()){
                    String key = (String) keys.nextElement();
                    String values = StringUtils.ans2GB(inve_config.getProperty(key));
                    String[] infos = values.split(",");
                    Inventory inv = new Inventory(++num,key,infos[0],infos[1]);
                    inventories.add(inv);
                }
            } finally {
                if(invis != null){
                    invis.close();
                    invis = null;
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return inventories;
    }*/
}
